﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMap.NET.Projections;
using GMap.NET;

namespace GoogleMapsAPI
{
    public partial class ucMaps : UserControl
    {
        MapSlate _currentState = new MapSlate();
        frmMain frm;
        //PictureBox[,] _slates = new PictureBox[2, 2];

        //int Num = 1;
        //int size = 640;

        Marker curredmark = new Marker();
        PointLatLng redmarkPos;
        Point curPos;
        PictureBox pb;
        public ucMaps(frmMain f)
        {
            InitializeComponent();
            frm = f;
            pb = new PictureBox();
            pb.Size = new Size(640, 640);
            pb.Location = new Point(0, 0);
            pb.BorderStyle = BorderStyle.None;
            this.Controls.Add(pb);
        }

        #region "event drag'n'drop"

        Point prevPos = new Point();

        private void pb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                prevPos = new Point(e.X, e.Y);
                Cursor = Cursors.SizeAll;
            }
            else if (e.Button == MouseButtons.Left)
            {
                curredmark.Dispose();
                Image marker = Properties.Resources.marker;
                curPos = new Point(e.X - marker.Width / 2, e.Y - marker.Height);
                drawMarker(marker, curPos);
            }
        }

        private void pb_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point deltaPos = new Point(e.X - prevPos.X, e.Y - prevPos.Y);
                curPos.X += deltaPos.X;
                curPos.Y += deltaPos.Y;
                MercatorProjection mp = new MercatorProjection();
                Point temp = mp.FromLatLngToPixel(0, 0, _currentState.Zoom);
                PointLatLng pll = mp.FromPixelToLatLng(temp.X - deltaPos.X, temp.Y - deltaPos.Y, _currentState.Zoom);
                _currentState.Latitude += pll.Lat;
                _currentState.Longitude += pll.Lng; 
                loadMap();

                Cursor = Cursors.Default;
            }
            
        }
        #endregion

        private void ucMaps_Load(object sender, EventArgs e)
        {
            curPos = new Point(310, 286);
            loadMap();
        }

        private void drawMarker(Image marker, Point mousePos)
        {
            curredmark.Dispose();
            curredmark = new Marker(marker, curPos);
            curredmark.MouseDown += new MouseEventHandler(pb_MouseDown);
            curredmark.MouseUp += new MouseEventHandler(pb_MouseUp);
            this.Controls.Add(curredmark);
            curredmark.BringToFront();

            Point deltaPos;

            deltaPos = new Point(curredmark.Width / 2 - curPos.X - marker.Width / 2, curredmark.Height / 2 - curPos.Y - marker.Height);
            MercatorProjection mp = new MercatorProjection();
            Point temp = mp.FromLatLngToPixel(0, 0, _currentState.Zoom);
            PointLatLng pll = mp.FromPixelToLatLng(temp.X - deltaPos.X, temp.Y - deltaPos.Y, _currentState.Zoom);
            redmarkPos.Lat = _currentState.Latitude + pll.Lat;
            redmarkPos.Lng = _currentState.Longitude + pll.Lng;
            frm.SetTextboxMarker(redmarkPos.Lat.ToString(), redmarkPos.Lng.ToString());
        }

        private void drawgreenMarker(Image marker, List<Data> data)
        {
            curredmark = new Marker(marker, curPos);
            curredmark.MouseDown += new MouseEventHandler(pb_MouseDown);
            curredmark.MouseUp += new MouseEventHandler(pb_MouseUp);
            this.Controls.Add(curredmark);
            curredmark.BringToFront();

            Point deltaPos;

            deltaPos = new Point(curredmark.Width / 2 - curPos.X - marker.Width / 2, curredmark.Height / 2 - curPos.Y - marker.Height);
            MercatorProjection mp = new MercatorProjection();
            Point temp = mp.FromLatLngToPixel(0, 0, _currentState.Zoom);
            PointLatLng pll = mp.FromPixelToLatLng(temp.X - deltaPos.X, temp.Y - deltaPos.Y, _currentState.Zoom);
            redmarkPos.Lat = _currentState.Latitude + pll.Lat;
            redmarkPos.Lng = _currentState.Longitude + pll.Lng;
            frm.SetTextboxMarker(redmarkPos.Lat.ToString(), redmarkPos.Lng.ToString());
        }

        public void loadMap()
        {
            MercatorProjection mp = new MercatorProjection();
            Point cp = mp.FromLatLngToPixel(_currentState.Latitude, _currentState.Longitude, _currentState.Zoom);

            PointLatLng pll = mp.FromPixelToLatLng(cp, _currentState.Zoom);
            pb.Image = MapSlate.GetMapImage(pll.Lat, pll.Lng, _currentState.Zoom, 640, frm.data);
            drawMarker(Properties.Resources.marker, curPos);

            frm.SetTextboxMap(_currentState.Latitude.ToString(), _currentState.Longitude.ToString());
        }

        public int GetZoom()
        {
            return _currentState.Zoom;
        }

        public Double GetLat()
        {
            return _currentState.Latitude;
        }

        public Double GetLng()
        {
            return _currentState.Longitude;
        }

        public void Zoom(Int32 z)
        {
            MercatorProjection mp = new MercatorProjection();
            Point cp1 = mp.FromLatLngToPixel(_currentState.Latitude, _currentState.Longitude, _currentState.Zoom);
            _currentState.Zoom = z;
            Point cp2 = mp.FromLatLngToPixel(_currentState.Latitude, _currentState.Longitude, _currentState.Zoom);

            loadMap();
        }
    }
}
